package cn.wen.yinxiu.member.server.socket.handler;

import cn.wen.yinxiu.member.server.protocol.friend.SearchFriendRequest;
import cn.wen.yinxiu.member.server.protocol.friend.SearchFriendResponse;
import cn.wen.yinxiu.member.service.UsersService;
import cn.wen.yinxiu.member.vo.LuckUserInfo;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName: SearchFriendHandler
 * @Author: 小飞
 * @Date: 2023/5/28 14:34
 * @Description: 查找朋友处理
 */
@Slf4j
public class SearchFriendHandler extends SimpleChannelInboundHandler<SearchFriendRequest> {

    private final UsersService userService;

    public SearchFriendHandler(UsersService userService) {
        this.userService = userService;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, SearchFriendRequest msg) throws Exception {
        log.info("搜索好友请求处理：{}", JSON.toJSONString(msg));
        List<LuckUserInfo> userInfoList = userService.queryFuzzyUserInfoList(msg.getAccount(), msg.getSearchKey());
        SearchFriendResponse response = new SearchFriendResponse();
        response.setList(userInfoList);
        Channel channel = channelHandlerContext.channel();
        channel.writeAndFlush(response);
    }
}
